/**
 * 获取字符串字节长度

 * @param {Object} val
 */
export function getByteSize(val) {
	var str = new String(val);
	var bytesCount = 0;
	for (var i = 0, n = str.length; i < n; i++) {
		var c = str.charCodeAt(i);
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			bytesCount += 1;
		} else {
			bytesCount += 2;
		}
	}
	return bytesCount;
}
/**
 * 根据字节长度截取
 * var 字符串
 * start 开始字节
 * length 截取长度
 */
export function subStrByByte(val, start, length) {
	var str = new String(val);
	let result = ''
	var bytesCount = 0;
	var startSize = 0;
	var handelSize = 0;
	for (var i = 0, n = str.length; i < n; i++) {
		const s = str[i]
		var c = str.charCodeAt(i);
		let charSize = 1
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			charSize = 1;
		} else {
			charSize = 2;
		}
		bytesCount += charSize
		if (bytesCount > start) {
			if (handelSize < length) {
				result += s
				handelSize += charSize
			}
		}

	}
	return result;
}

/**
 * 判断字符串空
 * @param {String} str
 */
export function isBlank(str) {
	if (str === undefined || str === null) {
		return true
	}
	if (str == '' || str.length == 0) {
		return true
	}
	if (typeof str === 'object') {
		try {
			str = JSON.stringify(str)
		} catch (e) {
			//TODO handle the exception
		}
	}
	if (str == 'undefined' || str == 'null' || str == '[]' || str == '{}') {
		return true
	}
	return false
}

/**
 * 判断字符串不空
 * @param {String} str
 */
export function isNotBlank(str) {
	return !isBlank(str)
}

/**
 *
 * 替换字符串里面的转义字符，比如\n \r
 * @param {Object} str
 */
export function textToHTML(string) {
	//替换所有的换行符
	string = string.replace(/\r\n/g, "<br>");
	string = string.replace(/\n/g, "<br>");

	//替换所有的空格（中文空格、英文空格都会被替换）
	string = string.replace(/\s/g, "&nbsp;");
	return string
}